package org.jspresso.framework.application.frontend.action.entity.creation;

import java.util.Map;

import org.jspresso.framework.action.IActionHandler;
import org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController;
import org.jspresso.framework.application.frontend.action.CloseDialogAction;
import org.jspresso.framework.model.component.IComponent;
import org.jspresso.framework.model.entity.IEntity;
import org.jspresso.model.ActionTestIo;
import org.jspresso.model.TestEntity;
import org.jspresso.tester.utils.TestEntitySerializer;

public class OkChoosenEntityCreationAction<E, F, G> extends CloseDialogAction<E, F, G> {

	@Override
	public boolean execute(IActionHandler actionHandler, Map<String, Object> context) {
		IComponent component = getModel(context);

	    if (actionHandler.execute(getWrappedAction(context), context)) {
	        getController(context).disposeModalDialog(getActionWidget(context), context);
	        ActionTestIo actionTestIoToAffect = getParentModel(context);
			TestEntitySerializer.serializeToStore(component, actionTestIoToAffect.getBackActionTest().getTestScope(), true, false, (HibernateBackendController) getBackendController(context));
			TestEntity testEntity = TestEntitySerializer.findTestEntity((component instanceof IEntity ? ((IEntity)component).getId() : String.valueOf(component.hashCode())), component.getComponentContract(), actionTestIoToAffect.getBackActionTest().getTestScope(), (HibernateBackendController) getBackendController(context));
			setActionParameter(testEntity, context);
	        if (getNextAction(context) != null) {
	          return actionHandler.execute(getNextAction(context), context);
	        }
	        return true;
	      }
		

		return true;
	}

}
